# -*- coding: utf-8 -*-
import requests,time,pymysql,random
import urllib.request,urllib.error
import os,re,IPy,xlrd
from multiprocessing import Pool


def domainT():
    s=requests.session()#创建会话对象s
    HEAD = { #请求head
        "Host": "113.215.20.11",
        "Connection": "keep-alive",
        "Content-Length": "151",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
        "Origin": "http://113.215.20.11",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Referer": "http://113.215.20.11/",
        "Accept-Encoding": "gzip, deflate"

    }
    #s.headers.update(HEAD)
    login=s.get("http://113.215.20.11") #先get一次，用来获取cookies
    #login=requests.get("http://113.215.20.11/manager/")
    csrftoken=str(login.cookies)#从cookies提取csrftoken
    print(csrftoken)
    csrftoken=re.findall("csrftoken\=\w+",csrftoken)[0].replace("csrftoken=","")
    print(csrftoken)

    DATA={
        "csrfmiddlewaretoken":csrftoken,#post数据中需要csrftoken的值
        "username":"ganlian",#用户名密码
        "password":"gl871020",
        "this_is_the_login_form":"1",
        "next":"/"
    }
    s.post("http://113.215.20.11",data=DATA,headers=HEAD)#post csrftoken，用户名密码来登录
    r=s.get("http://113.215.20.11/Reg/?format=json")#在用这个会话get这个json
    print(r.json())

if __name__ == '__main__':
    domainT()







